<?php
import("ORG.Util.Function");
import("@.Org.Utils");
import("@.Org.TopicsTool");


class TopicsAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
		
	/**
	 * 专题列表
	 * Enter description here ...
	 */
	public function topics_list(){
		
	    $sort_flag  		= 	sort_flag($list['filter']);
	    $this->assign($sort_flag['tag'], $sort_flag['img']);

	    $sql_topics_list 	=	get_sql_topic_list();
	    
	    $param = array(
				'result'	=> $sql_topics_list,			//分页用的数组或sql
				'listvar'	=> 'topic_list',				//分页循环变量
				'listRows'	=> 10,							//每页记录数
				//'parameter'=>'cat_id=' . $cat_id . "&intro_type=" . $intro_type . "&invalid=" . $invalid,//url分页后继续带的参数
				'target'	=> 'content',	    			//ajax更新内容的容器id，不带#
				'pagesId'	=> 'page',						//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
				'template'	=> 'Topics:topic_list',		//ajax更新模板
		);		
		$this->page_topics_list($param);
		
		$this->display('topic_list');
	}
	
	/**
	 * 专题分页列表
	 * Enter description here ...
	 */
	public function page_topics_list($param){
		extract($param);
		//总记录数
		$flag = is_string($result);
		
		$listvar = $listvar ? $listvar : 'list';
		$listRows = $listRows? $listRows : 21;
		if ($flag){
			$totalRows	=	count(M()->query($result));
		}			
		else{
			$totalRows = ($result) ? count($result) : 1;
		}
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');//要ajax分页配置PAGE中必须theme带%ajax%，其他字符串替换统一在配置文件中设置，
		//=========================================格式化数据
		$res = array();
		foreach($voList as $topic){
			$topic['start_time'] 	=	local_date('Y-m-d',$topic['start_time']);
			$topic['end_time']   	=	local_date('Y-m-d',$topic['end_time']);
			$topic['url']        	= 	/*$GLOBALS['ecs']->url() .*/ 'topic.php?topic_id=' . $topic['topic_id'];
			$res[] 					= 	$topic;
		}
		$voList						=	$res;
		//=========================================格式化数据
		//可以使用该方法前用C临时改变配置
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();		
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);
		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'ajaxlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}
	
	/**
	 * 修改主题
	 * 
	 */
	public function mod_topic(){

//		$GoodsModel		=	new GoodsModel();
//		
//	    $isadd     		= 	$_REQUEST['act'] == 'add';
//	    $this->assign('isadd', $isadd);
//	    $topic_id  		= 	empty($_REQUEST['topic_id']) ? 0 : intval($_REQUEST['topic_id']);
//	
//	    $this->assign('cat_list',   $GoodsModel->cat_list());
//	    $this->assign('brand_list', get_brand_list());
//	    $this->assign('cfg_lang',   $_CFG['lang']);
//	   // $this->assign('topic_style_color',   $topic_style_color);
//	
//	    $width_height 	= 	get_toppic_width_height();
//	    if(isset($width_height['pic']['width']) && isset($width_height['pic']['height']))
//	    {
//	        $this->assign('width_height', sprintf($_LANG['tips_width_height'], $width_height['pic']['width'], $width_height['pic']['height']));
//	    }
//	    if(isset($width_height['title_pic']['width']) && isset($width_height['title_pic']['height']))
//	    {
//	        $this->assign('title_width_height', sprintf($_LANG['tips_title_width_height'], $width_height['title_pic']['width'], $width_height['title_pic']['height']));
//	    }
//	
//	    if (!$isadd)
//	    {
//	        $sql = "SELECT * FROM " . $ecs->table('topic') . " WHERE topic_id = '$topic_id'";
//	        $topic = $db->getRow($sql);
//	        $topic['start_time'] = local_date('Y-m-d', $topic['start_time']);
//	        $topic['end_time']   = local_date('Y-m-d', $topic['end_time']);
//	
//	        create_html_editor('topic_intro', $topic['intro']);
//	
//	        require(ROOT_PATH . 'includes/cls_json.php');
//	
//	        $json          = new JSON;
//	        $topic['data'] = addcslashes($topic['data'], "'");
//	        $topic['data'] = $json->encode(@unserialize($topic['data']));
//	        $topic['data'] = addcslashes($topic['data'], "'");
//	
//	        if (empty($topic['topic_img']) && empty($topic['htmls']))
//	        {
//	            $topic['topic_type'] = 0;
//	        }
//	        elseif ($topic['htmls'] != '')
//	        {
//	            $topic['topic_type'] = 2;
//	        }
//	        elseif (preg_match('/.swf$/i', $topic['topic_img']))
//	        {
//	            $topic['topic_type'] = 1;
//	        }
//	        else
//	        {
//	            $topic['topic_type'] = '';
//	        }
//	
//	        $this->assign('topic', $topic);
//	        $this->assign('act',   "update");
//	    }
//	    else
//	    {
//	        $topic = array('title' => '', 'topic_type' => 0, 'url' => 'http://');
//	        $this->assign('topic', $topic);
//	
//	        create_html_editor('topic_intro');
//	        $this->assign('act', "insert");
//	    }
		$this->display('topic_edit');
	}
	
	
	
	
	
	
	
	
}







